package data.net;

import java.io.IOException;

import org.eclipse.swt.custom.StyledText;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.select.NodeTraversor;

public class ResearchHunt {
	StyledText txtResearchRpt;

	public ResearchHunt(StyledText stxt){
		txtResearchRpt=stxt;
	}
	
	public void refresh(int queryType) {
		if(queryType==1){
			String url = "http://www.newone.com.cn/research/list?id=2";
			
			Document doc = null;
			try {
				txtResearchRpt.setText("");
				doc = Jsoup.connect(url).get();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if(doc==null){
				return;
			}
			Element el = doc.getElementById("td5");
			Elements links = el.getElementsByTag("a");
			for (Element link : links) {  
				String linkHref = link.attr("href");  
				Document doc2 = null;
				try {
					doc2 = Jsoup.connect("http://www.newone.com.cn"+linkHref).get();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(doc2==null){
					return;
				}
				Elements tables = doc2.getElementsByClass("c_table1");
				boolean isPrint = false;
				for(Element table : tables){
					Elements tds = table.getElementsByTag("td");
					for(Element td : tds){
						String plainText = getPlainText(td);
						if(isPrint&&plainText.indexOf("强烈推荐-A")>0){
							txtResearchRpt.append(plainText+"\n\n");
							txtResearchRpt.append("========================================================================================\n\n");
						}
						isPrint = plainText.equals("摘要：");
					}
				}
			}
		}else if(queryType==2){
			String url = "http://finance.eastmoney.com/news/ccjdd.html";
			
			Document doc = null;
			try {
				txtResearchRpt.setText("");
				doc = Jsoup.connect(url).get();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(doc==null){
				return;
			}
			Elements el = doc.getElementsByClass("list");
			Elements links = el.get(0).getElementsByTag("a");
			for (Element link : links) {  
				String linkHref = link.attr("href");  
				String title = link.attr("title");
				boolean isCare = title.indexOf("政策")>=0|| title.indexOf("利")>=0|| title.indexOf("行业")>=0||title.indexOf("中标")>=0;
				if(!isCare){
					continue;
				}
				Document doc2 = null;
				try {
					doc2 = Jsoup.connect(linkHref).get();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if(doc2==null){
					return;
				}
				Element div = doc2.getElementById("ContentBody");
//				Elements divs2 = doc2.getElementsByClass("pop_cont");
//				for(Element div : divs2){
					String plainText = getPlainText(div);
					txtResearchRpt.append(title+"\n\n");
					txtResearchRpt.append(plainText+"");
					txtResearchRpt.append("\n\n========================================================================================\n\n");
//				}
			}
		}
		
	}
	
	public String getPlainText(Element element) {
        FormattingVisitor formatter = new FormattingVisitor();
        NodeTraversor traversor = new NodeTraversor(formatter);
        traversor.traverse(element); // walk the DOM, and call .head() and .tail() for each node

        return formatter.toString();
    }

}
